﻿Imports Q7P1TTS.Lib
Imports System.Text.RegularExpressions
Imports ComponentFactory.Krypton.Toolkit

Public Class FeedBackForm
    Dim fb As Complaint
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        fb = New Complaint()
    End Sub

    Public Sub New(ByVal ID As Integer)
        InitializeComponent()
        fb = ComplaintController.load(ID)
    End Sub
    Private Sub FeedBackForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.AcceptButton = Me.btnSubmit
        mainForm.CancelButton = Me.btnCancel
        If fb.ID > 0 Then
            mainForm.SetHeader("Update Feedback: " & fb.RefNumber)
        Else
            mainForm.SetHeader("Submit Feedback")
        End If
        
        cboFbCat.DataSource = CategoryController.load("Select...", 0)
        cboFbSource.DataSource = SourceController.load("Select...", 0)
        cboFbType.DataSource = TypeController.load("Select...", "0")

        cboStatusSelector.Items.Add(ComplaintStatus.Pending)
        cboStatusSelector.Items.Add(ComplaintStatus.UnderInvestigation)
        cboStatusSelector.Items.Add(ComplaintStatus.InvestigationCompleted)
        cboStatusSelector.Items.Add(ComplaintStatus.Closed)
        cboStatusSelector.Items.Add(ComplaintStatus.Cancelled)
        cboStatusSelector.SelectedIndex = 0
        cboStatusSelector.Enabled = (fb.ID > 0)

        btnInvestigation.Visible = fb.ID > 0 And fb.Status = ComplaintStatus.UnderInvestigation
        btnAction.Visible = fb.ID > 0 And fb.Investigations.Count > 0
        If fb.ID > 0 Then
            Try
                lblCustRefNo.Text = fb.Customer.Account
                dpFbDate.Value = ConvertHelper.ToDateTime(fb.SubmitOn)
                dpIncidentDate.Value = ConvertHelper.ToDateTime(fb.IncidentDate)
                cboFbCat.SelectedIndex = cboFbCat.Items.IndexOf(fb.Category)
                cboFbSource.SelectedIndex = cboFbSource.Items.IndexOf(fb.Source)
                cboFbType.SelectedIndex = cboFbType.Items.IndexOf(fb.Type)
                txtBusStop.Text = fb.BusStop
                txtDescription.Text = fb.Description
                txtPlaceOfIncident.Text = fb.PlaceOfIncident
                txtRegNo.Text = fb.Vehicle
                cboStatusSelector.SelectedIndex = cboStatusSelector.Items.IndexOf(fb.Status)
            Catch ex As Exception

            End Try
            
        Else
            Dim cust = SessionHelper.Sessions("USER_INFO")
            If TypeOf (cust) Is Customer Then
                lblCustRefNo.Text = cust.Account
            End If
        End If
        btnSubmit.Visible = mainForm.isCREmployeeStaff Or mainForm.isCREmployeeLead Or mainForm.isAdmin
    End Sub

    Private Function validateData() As Boolean
        Dim res As Boolean = True
        If cboFbCat.SelectedValue = 0 Then
            errorProvider.SetError(cboFbCat, "Please select a category.")
            res = res And False
        Else
            errorProvider.SetError(cboFbCat, "")
        End If
        If cboFbSource.SelectedValue = 0 Then
            errorProvider.SetError(cboFbSource, "Please select a source.")
            res = res And False
        Else
            errorProvider.SetError(cboFbSource, "")
        End If
        If cboFbType.SelectedValue.ToString().Trim() = "0" Then
            errorProvider.SetError(cboFbType, "Please select a type.")
            res = res And False
        Else
            errorProvider.SetError(cboFbType, "")
        End If
        If dpIncidentDate.Value > dpFbDate.Value Then
            errorProvider.SetError(dpIncidentDate, "Invalid incident date.")
            res = res And False
        Else
            errorProvider.SetError(dpIncidentDate, "")
        End If
        If String.IsNullOrEmpty(txtPlaceOfIncident.Text.Trim()) Then
            errorProvider.SetError(txtPlaceOfIncident, "Place of Incident can not be left blank.")
            res = res And False
        Else
            errorProvider.SetError(txtPlaceOfIncident, "")
        End If
        Return res
    End Function

    Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        If validateData() Then
            If fb.ID <= 0 Then
                fb.SubmitOn = ConvertHelper.ToUnixTimestamp(dpFbDate.Value)
                Dim cust As Customer = SessionHelper.Sessions("USER_INFO")
                If TypeOf (cust) Is Customer Then
                    fb.SubmitBy = cust.ID
                End If
                fb.Status = ComplaintStatus.Pending
            Else
                fb.Status = cboStatusSelector.SelectedItem.ToString()
            End If

            fb.CatID = cboFbCat.SelectedValue
            fb.SourceID = cboFbSource.SelectedValue
            fb.TypeID = cboFbType.SelectedValue

            fb.PlaceOfIncident = txtPlaceOfIncident.Text.Trim()
            fb.BusStop = txtBusStop.Text.Trim()
            fb.Description = txtDescription.Text.Trim()
            fb.IncidentDate = ConvertHelper.ToUnixTimestamp(dpIncidentDate.Value)
            fb.RefNumber = ConvertHelper.GenerateAccount(cboFbType.SelectedValue.ToString().Trim())
            fb.SubmitOn = ConvertHelper.ToUnixTimestamp(DateTime.Now())
            fb.Vehicle = txtRegNo.Text.Trim()
            If ComplaintController.save(fb) > 0 Then
                Dim mainForm As MainForm = Me.FindForm()
                If mainForm.isCustomer Then
                    If (MessageBox.Show("Thank you! Your feedback has been sent to our operational department and will be process as soon as possible.") = DialogResult.OK) Then
                        mainForm.GoToForm(New Welcome(), "Welcome")
                    End If
                Else
                    mainForm.GoToForm(New FeedbackList(0))
                End If
               
            Else
                MessageBox.Show("Sorry! There was an error while saving data. Please try again later.", "Submit Feedback Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Dim mainForm As MainForm = Me.FindForm()
        If mainForm.isCREmployeeStaff Or mainForm.isCREmployeeLead Or mainForm.isAdmin Then
            mainForm.GoToForm(New FeedbackList(0), "Complaint List")
        ElseIf mainForm.isOPEmployeeStaff Then
            mainForm.GoToForm(New FeedbackList(1), "My Assigned Complaint")
        ElseIf mainForm.isCustomer Then
            mainForm.GoToForm(New Welcome(), "Welcome")
        End If

    End Sub

    Private Sub btnInvestigation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvestigation.Click
        Dim mainForm As MainForm = Me.FindForm()
        mainForm.GoToForm(New InvestigationForm(fb.ID), "Enter Investigation")
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        For Each c As Control In KryptonPanel1.Controls
            If TypeOf c Is KryptonTextBox Then
                Dim txt As KryptonTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonRichTextBox Then
                Dim txt As KryptonRichTextBox = c
                txt.Text = ""
            End If
            If TypeOf c Is KryptonComboBox Then
                Dim cbo As KryptonComboBox = c
                cbo.SelectedIndex = 0
            End If
        Next
    End Sub
End Class
